package peer.gui;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SpringLayout;

import peer.NetworkManager.NetworkManager;


public class NetworkPanel extends JPanel implements ActionListener
{
	JLabel serverStatusLabel;
	JTextField serverIPTextField;
	NetworkConnectionModel connectionModel;
	
	private boolean connected = false;
	
	public NetworkPanel()
	{
		SpringLayout layout = new SpringLayout();
		setLayout(layout);
		
		JLabel serverIPLabel = new JLabel("Server IP Address");
		serverIPTextField = new JTextField(10);
		JButton serverConnectButton = new JButton("Connect");
		serverConnectButton.setActionCommand("connect");
		serverConnectButton.addActionListener(this);
		JButton getPeersButton = new JButton("Get Peers");
		getPeersButton.setActionCommand("get peers");
		getPeersButton.addActionListener(this);
		serverStatusLabel = new JLabel("not connected");
		serverStatusLabel.setPreferredSize(new Dimension(120, 20));
		add(serverIPLabel);
		add(serverIPTextField);
		add(serverConnectButton);
		add(getPeersButton);
		add(serverStatusLabel);
		layout.putConstraint(SpringLayout.WEST, serverIPLabel, 5, SpringLayout.WEST, this);
		layout.putConstraint(SpringLayout.NORTH, serverIPLabel, 5, SpringLayout.NORTH, this);
		layout.putConstraint(SpringLayout.WEST, serverIPTextField, 5, SpringLayout.EAST, serverIPLabel);
		layout.putConstraint(SpringLayout.NORTH, serverIPTextField, 0, SpringLayout.NORTH, serverIPLabel);
		layout.putConstraint(SpringLayout.WEST, serverConnectButton, 5, SpringLayout.EAST, serverIPTextField);
		layout.putConstraint(SpringLayout.NORTH, serverConnectButton, 0, SpringLayout.NORTH, serverIPTextField);
		layout.putConstraint(SpringLayout.WEST, getPeersButton, 5, SpringLayout.EAST, serverConnectButton);
		layout.putConstraint(SpringLayout.NORTH, getPeersButton, 0, SpringLayout.NORTH, serverIPTextField);
		layout.putConstraint(SpringLayout.WEST, serverStatusLabel, 5, SpringLayout.EAST, getPeersButton);
		layout.putConstraint(SpringLayout.NORTH, serverStatusLabel, 0, SpringLayout.NORTH, getPeersButton);
		layout.putConstraint(SpringLayout.EAST, this, 5, SpringLayout.EAST, serverStatusLabel);
		
		connectionModel = new NetworkConnectionModel();
		JTable connectionTable = new JTable(connectionModel);
		connectionModel.addTableModelListener(connectionTable);
		JScrollPane connectionPane = new JScrollPane(connectionTable);
		add(connectionPane);
		layout.putConstraint(SpringLayout.WEST, connectionPane, 5, SpringLayout.WEST, this);
		layout.putConstraint(SpringLayout.NORTH, connectionPane, 5, SpringLayout.SOUTH, getPeersButton);
		layout.putConstraint(SpringLayout.EAST, connectionPane, 0, SpringLayout.EAST, serverStatusLabel);
		layout.putConstraint(SpringLayout.SOUTH, this, 5, SpringLayout.SOUTH, connectionPane);
	}

	public void actionPerformed(ActionEvent e)
	{
		if(e.getActionCommand().equals("connect"))
		{
			serverStatusLabel.setText("connecting...");
			NetworkManager nm = NetworkManager.instance(); 
			nm.connectToNetwork(serverIPTextField.getText());
			connected = true;
		}
		else if(e.getActionCommand().equals("get peers"))
		{
			if(connected)
			{
				serverStatusLabel.setText("getting peers...");
				NetworkManager nm = NetworkManager.instance(); 
				nm.requestPeerList();
			}
			else
			{
				serverStatusLabel.setText("must connect first");
			}
		}
		
	}
	
	public void connectionAccepted(boolean connected)
	{
		this.connected = connected;
		if(connected)
		{
			serverStatusLabel.setText("connected");
		}
		else
		{
			serverStatusLabel.setText("unable to connect");
		}
	}
	
	public void addNewPeers(String addresses)
	{
		if(addresses != null)
		{
			if(addresses.equals(""))
			{
				serverStatusLabel.setText("no other peers");
			}
			else
			{
				String[] array = addresses.split(";");
				serverStatusLabel.setText("got peer list");
				for (String address : array)
				{
					String[] split = address.split(":");
					connectionModel.addConnection(split[0], Integer.parseInt(split[1]));
				}
			}
		}
		else
		{
			serverStatusLabel.setText("no other peers");
		}
	}

	public void addConnection(String address, int port)
	{
		connectionModel.addConnection(address, port);
	}

	/*public boolean removeConnection(String address, int port)
	{
		return connectionModel.removeConnection(address, port);
		
	}*/

	public void setConnectionStatus(String status)
	{
		serverStatusLabel.setText(status);
	}
}
